package com.sbm.merge.设计模式.decorator;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.aop.framework.AopProxyUtils;

import java.util.List;


public class BasicMessgeSenderDecorator extends MessageManager{

    private final MessageManager sender;

    private String combinationType;

    public BasicMessgeSenderDecorator(MessageManager sender) {
        this.sender = sender;
        this.combinationType = MessageManagerFactory.getComboMapKey(getNameList());
    }

    public String getCombinationType() {
        return combinationType;
    }

    public void send(String message) {
       this.sender.sendMessage(message);
    }



    private List<String> getNameList() {
        Object target = this.getRealTargetObject(this);
        Object targetDecoratorObj = this.getRealTargetObject(this.sender);
        if (targetDecoratorObj instanceof BasicMessgeSenderDecorator) {
            List<String> nameList = ((BasicMessgeSenderDecorator)targetDecoratorObj).getNameList();
            nameList.add(target.getClass().getSimpleName());
            return nameList;
        } else {
            return Lists.newArrayList(target.getClass().getSimpleName());
        }
    }

    private Object getRealTargetObject(Object currentObj) {
        Object target = AopProxyUtils.getSingletonTarget(currentObj);
        return ObjectUtils.defaultIfNull(target, currentObj);
    }
}
